# coding: utf-8
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.http import Http404
from BigPrize.GameRule import BP
from django.utils import simplejson
import base64

def Buy(request,Ticket = None):
    """Method To Buy A BigPrize Ticket"""
    if request.method == 'GET':
        if Ticket is not None:
            Ticket = base64.b64decode(Ticket)
        LogedIn = 'token' in request.session
        if(LogedIn):
            UserName = request.session['username']
        return render_to_response('BigPrize/Buy.html',locals())
    else:
        Result = {"Success":True,"Message":""}
        if 'Ticket' in request.POST:
            Ticket = request.POST['Ticket']
            EncodedTicket = BP.BigPrize.Encode(Ticket)
            TicketCount = BP.BigPrize.Count(EncodedTicket)
            Result["Success"] = True
            Result["Message"] = u"购买成功"
        else:
            Result["Success"] = False
            Result["Message"] = u"无法提交空彩票"
        return HttpResponse(simplejson.dumps(Result),mimetype="text/json")

def Count(request):
    """Method To Calculate The Ticket Count"""
    if request.method == 'GET' and 'Ticket' in request.GET:
        Ticket = request.GET['Ticket']
        Ticket = BP.BigPrize.Encode(Ticket)
        return HttpResponse(BP.BigPrize.Count(Ticket))
    else:
        raise Http404